﻿using MediatR;
using Process.Api.Application.Command;
using Process.BLL;
using Process.ErrorCod;
using Process.Model;
using ProcessDto;

namespace Process.Api.Application.Handler
{
    public class ShowProcessinfoHandler : IRequestHandler<ShowProcessinfoCommand, ApiPageing<ProcessInfoDto>>
    {
        private readonly IProcessRepository<Processinfo> _processinfoRepository;
        private readonly IProcessRepository<ProcessType> _processTypeRepository;
        private readonly ILogger<ShowProcessinfoHandler> _logger;

        public ShowProcessinfoHandler(IProcessRepository<Processinfo> processinfoRepository, IProcessRepository<ProcessType> processTypeRepository, ILogger<ShowProcessinfoHandler> logger)
        {
            _processinfoRepository = processinfoRepository;
            _processTypeRepository = processTypeRepository;
            _logger = logger;
        }
        /// <summary>
        /// 获取工序管理列表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<ApiPageing<ProcessInfoDto>> Handle(ShowProcessinfoCommand request, CancellationToken cancellationToken)
        {
            ApiPageing<ProcessInfoDto>result = new ApiPageing<ProcessInfoDto>();

            var info = _processinfoRepository.GetAllAsync().Where(x => x.IsDelete == false);

            var type = _processTypeRepository.GetAllAsync().Where(x => x.IsDelete == false);

            var list = from a in info
                       join b in type on a.ProcessType_Id equals b.Id
                       select new ProcessInfoDto
                       {
                           Id=a.Id,
                           ProcessCode=a.ProcessCode,
                           ProcessName=a.ProcessName,
                           ProcessType_Id=a.ProcessType_Id,
                           ProcessTypeName=b.ProcessTypeName,
                           ProcessStatus=a.ProcessStatus,
                           Responsible=a.Responsible,
                           Department=a.Department,
                           Remark= a.Remark
                       };
            if(!string.IsNullOrEmpty(request.ProcessCode))
            {
                list =list.Where(x=>x.ProcessCode==request.ProcessCode);
            }
            if(!string.IsNullOrEmpty(request.ProcessName))
            {
                list = list.Where(x => x.ProcessName.Contains(request.ProcessName));
            }
            if(!string.IsNullOrEmpty(request.Department))
            {
                list = list.Where(x => x.Department == request.Department);
            }
            var total = list.Count();
            var page = (int)Math.Ceiling(total * 1.0 / request.PageSize);
            var query =list.OrderByDescending(x=>x.Id).Skip((request.PageIndex-1)*request.PageSize).Take(request.PageSize).ToList();

            result.Code=ErrorStatus.Success;
            result.TotalCount=total;
            result.TotalPage=page;
            result.PageData = query;
            result.Message = "获取工序分页数据成功";
            return Task.FromResult(result);

        }
    }
}
